package fhp.hlhj.giantfold.activity.login

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Handler
import android.os.Message
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.interfaces.IForget
import fhp.hlhj.giantfold.javaBean.ForgetBean
import fhp.hlhj.giantfold.javaBean.GetSmsBean
import fhp.hlhj.giantfold.presenter.ForgetPresenter
import fhp.hlhj.giantfold.utils.LoadingUtils
import fhp.hlhj.giantfold.utils.MyUtils
import fhp.hlhj.giantfold.utils.ToastUtils
import kotlinx.android.synthetic.main.activity_forget_aty.*

class ForgetAty : BaseActivity(),IForget {

    private lateinit var forgetPresenter:ForgetPresenter
    private var sec = 60
    private val handler= @SuppressLint("HandlerLeak")
    object :Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            btGetYzm.isClickable=false
            sec--
            btGetYzm.text=sec.toString()+"秒"
            if (sec<1){
                btGetYzm.text="获取验证码"
                btGetYzm.isClickable=true
            }else{
                this.sendEmptyMessageDelayed(1,1000)
            }
        }
    }

    override fun getContentId(): Int = R.layout.activity_forget_aty


    override fun initView() {
        forgetPresenter=ForgetPresenter(this)
    }

    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }

        btGetYzm.setOnClickListener {
            val params = HttpParams()
            params.put("phone",etPhone.text.toString())
            params.put("sms_key",MyUtils.getMD5("test123456"))
            params.put("sms_type","1")
            forgetPresenter.getSms(params)
        }

        btNext.setOnClickListener {
            val pa = HttpParams()
            pa.put("phone",etPhone.text.toString())
            pa.put("sms_code",etYzm.text.toString())
            forgetPresenter.smsOk(pa)
        }
    }
    /**
     * 载入
     */
    override fun showLoading() {
        loadingDialog.show()
    }
    /**
     * 获取短信验证码
     */
    override fun getSMS(getSmsBean: GetSmsBean) {
        loadingDialog.dismiss()
        if (getSmsBean.code==200){
            handler.sendEmptyMessage(1)
        }else{
            ToastUtils.toast(this,"获取验证码错误")
        }
    }

    /**
     *短信验证码验证
     */
    override fun smsOk(forgetBean: ForgetBean) {
        if (forgetBean.code==200){
            val intent = Intent(this@ForgetAty, Forget2Aty::class.java)
            intent.putExtra("phone",etPhone.text.toString())
            startActivity(intent)
            finish()
        }else{
            ToastUtils.toast(this,"验证码错误，请重试")
        }
    }

}
